<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-input
          placeholder="输入书籍id查询"
          v-model="page.bookId"
          maxlength="20"
        >
        </el-input>
      </el-col>
      <el-col :span="2">
        <el-button icon="el-icon-search" @click="search" type="success"
          >搜索
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button icon="el-icon-delete" @click="reset" type="info"
          >重置
        </el-button>
      </el-col>
    </el-row>
    <el-table :data="classesList" border>
      <el-table-column prop="id" label="书籍类别id" align="center">
      </el-table-column>
      <el-table-column prop="bookId" label="书籍id" align="center">
      </el-table-column>
      <el-table-column prop="id" label="分类id" align="center">
      </el-table-column>
      <el-table-column prop="className" label="分类名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-popconfirm
            title="确认删除？"
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            @confirm="handleDelete(scope.row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              size="small"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[6, 8,21, 27, 36]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="page.total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getBookClasses,deleteClassesById } from "../api/bookClassesMG";
export default {
  name: "BookClasses",
  data() {
    return {
      page: {
        currentPage: 1,
        pageSize: 6,
        total: 20,
        bookId: "",
      },
      classesList: [],
    };
  },
  methods: {
    reset(){
      //点击重置
      this.page.currentPage = 1;
      this.page.bookId="",
      this.getClassesList();
    },
    search() {
      //点击搜索
      this.page.currentPage = 1;
      this.getClassesList();
    },
    async handleDelete(id) {
      // console.log(id)
      //删除选中的分类
      await deleteClassesById(id).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "success",
          });
          this.getClassesList();
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    async getClassesList() {
      //获取所有分类的列表
      await getBookClasses(this.page).then((res) => {
        if (res.data.code == 200) {
          this.page.total = res.data.data.total;
          this.classesList = res.data.data.list;
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    handleSizeChange(pageSize) {
      //改变每页尺寸触发事件
      this.page.pageSize = pageSize;
      //重新获取列表数据
      this.getClassesList();
    },
    handleCurrentChange(currentPage) {
      //改变当前页也会触发事件
      this.page.currentPage = currentPage;
      //重新获取列表数据
      this.getClassesList();
    },
  },
  mounted() {
    this.getClassesList();
  },
};
</script>
  <style lang="scss" scoped>
</style>